Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement constant optimization #116

Merged
merged 3 commits into from
Mar 4, 2024
Merged

Conversation

vacantron
Copy link
Collaborator

Consider the following scenario:

int a = 2;            /* constant assingment */
int b = a;            /* assignment via constant representation */
int c = a + b;
int d = c + 8;        /* mixed assigment */
return a + b + c + d; /* chained assignment */

The current code generator emits the assembly below, even though the variables a, b, c, d are all constant representations.

mov     r0, #2
mov     r1, r0
mov     r0, r1
add     r3, r1, r0
mov     r2, #8
add     r2, r3, r2
add     r4, r1, r0
add     r0, r4, r3
add     r1, r0, r2
mov     r0, r1

The new constant optimizer will evaluate the constant representations, trim the unused variables, and emit mov r0, #20 only.

Consider the following scenario:

```
int a = 2;            /* constant assingment */
int b = a;            /* assignment via constant representation */
int c = a + b;
int d = c + 8;        /* mixed assigment */
return a + b + c + d; /* chained assignment */
```

The current code generator emits the assembly below, even though the
variables a, b, c, d are all constant representations.

```
mov     r0, #2
mov     r1, r0
mov     r0, r1
add     r3, r1, r0
mov     r2, sysprog21#8
add     r2, r3, r2
add     r4, r1, r0
add     r0, r4, r3
add     r1, r0, r2
mov     r0, r1
```

The new constant optimizer will evaluate the constant representations,
trim the unused variables, and emit `mov r0, sysprog21#20` only.
Copy link
Collaborator

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add related test cases to verify the effectiveness of the proposed optimizer.

@vacantron
Copy link
Collaborator Author

Add related test cases to verify the effectiveness of the proposed optimizer.

The test cases are implicitly included in the existent ones, e.g.

shecc/tests/driver.sh

Lines 81 to 83 in 393b441

# arithmetic
expr 42 "24 + 18"
expr 30 "58 - 28"

Should we add more?

@jserv
Copy link
Collaborator

jserv commented Mar 4, 2024

The test cases are implicitly included in the existent ones, e.g.

shecc/tests/driver.sh

Lines 81 to 83 in 393b441

# arithmetic
expr 42 "24 + 18"
expr 30 "58 - 28"

Should we add more?

Yes, append the new items which reflect the chained assignments.

@jserv jserv merged commit d5dcdbd into sysprog21:master Mar 4, 2024
4 checks passed
@jserv
Copy link
Collaborator

jserv commented Mar 4, 2024

Thank @vacantron for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants